//
//  MoreButtonView.m
//  
//
//  Created by 尤振宇 on 16/9/23.
//
//

#import "MoreButtonView.h"
#import "Tool.h"
#import "UIView+extension.h"
//#import <Masonry.h>

@interface MoreButtonView ()
@property(nonatomic,strong)NSMutableArray <UIButton *>*buttonArr;

@end

@implementation MoreButtonView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self config];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self config];
    }
    return self;
}

-(void)config
{
    [self addSubview:self.scrollView];
    
    self.buttonCount = 2;
    self.lineColor = [UIColor blackColor];
    self.titleColor = [UIColor blackColor];
    self.selectedTitleColor = [UIColor blackColor];
    self.buttonW = 0;
    self.lineW = 0;
    self.lineH = 0;
    self.backImage = [UIImage imageNamed:@"clear"];
    self.selectedbackImage = [UIImage imageNamed:@"clear"];
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = self.lineColor;
    [self.scrollView addSubview:view];
    self.lineView = view;
}

-(UIButton *)buttonWithIndex:(int)index
{
    if (index >= self.buttonArr.count || index < 0) {
        return nil;
    }
    return self.buttonArr[index];
}

-(void)moveLine:(UIButton *)button
{
    for (UIButton *button in self.buttonArr) {
        [button setSelected:NO];
    }
    [button setSelected:YES];
    
    [UIView animateWithDuration:0.15 animations:^{
        if (self.lineWEqualTitleW) {
            self.lineView.width = [[button titleForState:(UIControlStateNormal)] widthForFontSize:button.titleLabel.font.pointSize andHeight:button.height];
        }
        self.lineView.centerX = button.centerX;
    } completion:^(BOOL finished) {
        if (finished) {
            if ([self.moreButtonViewDelegate respondsToSelector:@selector(lineAnimateDidEnd:)]) {
                [self.moreButtonViewDelegate lineAnimateDidEnd:self];
            }
        }
    }];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    CGFloat buttonW = self.buttonW ? self.buttonW : self.width / self.buttonCount;
    for (int i = 0; i<self.buttonArr.count; i++) {
        UIButton *button = [self.buttonArr objectAtIndex:i];
        button.frame = CGRectMake(i * buttonW, 0, buttonW, self.scrollView.height);
        
        if (i == 0) {
            [button setSelected:YES];
            CGFloat lineH = self.lineH ? self.lineH : 1;
            CGFloat lineW = self.lineW ? self.lineW : buttonW;
            if (self.lineWEqualTitleW) {
                lineW = [[button titleForState:(UIControlStateNormal)] widthForFontSize:button.titleLabel.font.pointSize andHeight:button.height];
            }
            self.lineView.frame = CGRectMake(0, self.scrollView.height - lineH + _lineYOffset, lineW, lineH);
            self.lineView.centerX = button.centerX;
        }
    }

    [self.scrollView setContentSize:(CGSizeMake(buttonW * self.buttonCount, self.height))];
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}

-(void)setSelectedButton:(NSInteger)index
{
    [self buttonAction:[self.buttonArr objectAtIndex:index]];
}

-(void)setButtonCount:(int)buttonCount
{
    _buttonCount = buttonCount;
    for (UIButton *button in self.buttonArr) {
        [button removeFromSuperview];
    }
    [self.buttonArr removeAllObjects];
    
    for (int i = 0; i < buttonCount; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setAdjustsImageWhenHighlighted:NO];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitleColor:self.titleColor forState:(UIControlStateNormal)];
        [button setTitleColor:self.selectedTitleColor forState:(UIControlStateSelected)];
        [button setBackgroundImage:self.backImage forState:(UIControlStateNormal)];
        [button setBackgroundImage:self.selectedbackImage forState:(UIControlStateSelected)];
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.scrollView addSubview:button];
        [self.buttonArr addObject:button];
    }
    [self.scrollView bringSubviewToFront:self.lineView];
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator =NO;
    }
    return _scrollView;
}

-(void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
    _lineView.backgroundColor = lineColor;
}

-(void)setTitles:(NSArray <NSString *>*)titles
{
    if (titles.count != self.buttonCount) {
        self.buttonCount = titles.count;
    }
    for (int i = 0; i < self.buttonArr.count; i++) {
        UIButton *button = self.buttonArr[i];
        NSString *title = titles[i];
        
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateSelected];
    }
}

-(void)setBackImage:(UIImage *)backImage
{
    _backImage = backImage;
    for (UIButton *button in self.buttonArr) {
        [button setBackgroundImage:_backImage forState:(UIControlStateNormal)];
    }
}

-(void)setSelectedbackImage:(UIImage *)selectedbackImage
{
    _selectedbackImage = selectedbackImage;
    for (UIButton *button in self.buttonArr) {
        [button setBackgroundImage:_selectedbackImage forState:(UIControlStateSelected)];
    }
}

-(void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    for (UIButton *button in self.buttonArr) {
        [button setTitleColor:titleColor forState:(UIControlStateNormal)];
    }
}

-(void)setSelectedTitleColor:(UIColor *)selectedTitleColor
{
    _selectedTitleColor = selectedTitleColor;
    for (UIButton *button in self.buttonArr) {
        NSLog(@"%@",button);
        [button setTitleColor:selectedTitleColor forState:(UIControlStateSelected)];
    }
}

-(void)buttonAction:(UIButton *)button
{
    if ([self.moreButtonViewDelegate respondsToSelector:@selector(touchUpInsideButtonActionIndex:title:sameIndex:)]) {
        NSInteger index = [self.buttonArr indexOfObject:button];
        [self.moreButtonViewDelegate touchUpInsideButtonActionIndex:index title:button.titleLabel.text sameIndex:self.lineView.frame.origin.x == button.frame.origin.x?YES:NO];
    }
    
    [self moveLine:button];
}

-(void)setLineYOffset:(CGFloat)lineYOffset
{
    _lineYOffset = lineYOffset;
    self.lineView.y = self.lineView.y + lineYOffset;
}

-(NSMutableArray *)buttonArr
{
    if (!_buttonArr) {
        _buttonArr = [NSMutableArray array];
    }
    return _buttonArr;
}

-(void)dealloc
{
    NSLog(@"MoreButtonView dealloc");
}


@end
